package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbShoppingCart;
import com.beetlestech.nfe.main.biz.com.dto.ShoppingCartInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.CoursediscountDTO;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.ShoppingcartDTO;
import com.beetlestech.nfe.main.db.tbl.StudentorderDTO;
import com.beetlestech.nfe.main.db.view.VshoppingcartDTO;

public class HbShoppingCartImpl extends AbstractDBLogic implements HbShoppingCart, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "订单信息表";

	public ShoppingCartInfoDTO[] getShoppingCar(Integer studentID){
		List<ShoppingCartInfoDTO> shoppingCartInfoList = new ArrayList<ShoppingCartInfoDTO>();
		VshoppingcartDTO searchDTO = new VshoppingcartDTO();
		searchDTO.setstudentid(studentID);
		List<VshoppingcartDTO> shoppingcart = queryDAO.queryForObjectList(VshoppingcartDTO.EQUAL_SEARCH, searchDTO);
		for(VshoppingcartDTO item : shoppingcart){
			shoppingCartInfoList.add(convertToShoppingCartInfo(item));
		}
		
		return shoppingCartInfoList.toArray(new ShoppingCartInfoDTO[shoppingCartInfoList.size()]);
	}
	
	public Integer createOrder(Integer[] shoppingCartID, Integer[] courseID, Integer studentID,String phone,Integer paymenttype){
		StudentorderDTO studentOrder = new StudentorderDTO();
		studentOrder.setstudentid(studentID);
		studentOrder.setgoodstype(Integer.parseInt(GOODS_TYPE.COURSE.toString()));
		studentOrder.setprice(computePrice(courseID));
		studentOrder.setpaymenttype(paymenttype);
		studentOrder.setstate(Integer.parseInt(CATAGORY_P_ORDER_STATE.SUBMITTED.toString()));
		studentOrder.setorderdate(getCurrentDate());
		studentOrder.setordertime(getCurrentTime());
		studentOrder.setphone(phone);
		updateDAO.updateOne(StudentorderDTO.INSERT, studentOrder, M10190WS, SH_INFO);
		
		for(Integer id: courseID){
			createOrderDetail(studentOrder.getorderid(), id);
		}

		for (Integer id : shoppingCartID) {
			ShoppingcartDTO shoppingDTO = new ShoppingcartDTO();
			shoppingDTO.setshoppingcartid(id);
			if (null != queryDAO.queryForObject(ShoppingcartDTO.EQUAL_SEARCH, shoppingDTO)) {
				deleteShoppingCart(id);
			}
		}
		return studentOrder.getorderid();
	}
	
	public void deleteShoppingCart(Integer shoppingCartID){
		ShoppingcartDTO deleteDTO = new ShoppingcartDTO();
		deleteDTO.setshoppingcartid(shoppingCartID);
		updateDAO.updateOne(ShoppingcartDTO.DELETE, deleteDTO, M10190WS, SH_INFO);
	}
	
	public void insertShoppingCart(Integer studentID, Integer courseID){
		ShoppingcartDTO shoppingcart = new ShoppingcartDTO();
		shoppingcart.setstudentid(studentID);
		shoppingcart.setcourseid(courseID);
		shoppingcart.setcreatedate(getCurrentDate());
		shoppingcart.setcreatetime(getCurrentTime());
		updateDAO.updateOne(ShoppingcartDTO.INSERT, shoppingcart, M10190WS, SH_INFO);
	}
	
	public Double computePrice(Integer[] courseID){
		Double sum = 0.0;
		Integer iCount = 0;
		for(Integer id: courseID){
			MstcourseDTO searchDTO = new MstcourseDTO();
			searchDTO.setcourseid(id);
			MstcourseDTO pircedto = queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, searchDTO);
			
			sum+=(pircedto.getprice() * pircedto.getdiscount());
			iCount++;
		}
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, CoursediscountDTO.COLUMN_START_COUNT, StatementUtil.KEYWORD.LESS_EQUAL_THAN, iCount);
		StatementUtil.andWhere(statementDTO, CoursediscountDTO.COLUMN_END_COUNT, StatementUtil.KEYWORD.GREATER_EQUAL_THAN, iCount);

		CoursediscountDTO search = new CoursediscountDTO();
		search.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		CoursediscountDTO dto = queryDAO.queryForObject(CoursediscountDTO.SQL_SEARCH, search);
		if(dto != null){
			sum = sum * dto.getdiscount();
		}
		
		return sum;
	}
	
	private void createOrderDetail(Integer orderID, Integer courseID){
		OrderdetailDTO orderdetail = new OrderdetailDTO();
		orderdetail.setorderid(orderID);
		orderdetail.setgoodsid(courseID);
		orderdetail.setstate(Integer.parseInt(LEARNING_STATE.NOT_GENERATE.toString()));
		updateDAO.updateOne(OrderdetailDTO.INSERT, orderdetail, M10190WS, SH_INFO);
	}
	
	private ShoppingCartInfoDTO convertToShoppingCartInfo(VshoppingcartDTO item) {
		ShoppingCartInfoDTO dto = new ShoppingCartInfoDTO();
		dto.setCourseID(item.getcourseid());
		dto.setName(item.getname());
		dto.setPath(item.getpath());
		dto.setShoppingCartID(item.getshoppingcartid());
		dto.setPrice(item.getprice() * item.getdiscount());
		dto.setCourseStart(item.getcoursestart());
		dto.setCourseEnd(item.getcourseend());
		dto.setCoursePeriod(item.getcourseperiod());
		
		return dto;
	}
}
